import CodingType from './CodingType.js';
import CodingMethod from './CodingMethod.js';
import CodingMethodParameter from './CodingMethodParameter.js';

export default class CodingProperty {
    /**
     * @type {[String]}
     */
    annotations = [];
    /**
     * @type {CodingType}
     */
    type = null;
    name = '';
    /**
     * 是否枚举常量
     * @type {boolean}
     */
    constant = false;
    caption = '';

    constructor(type, name) {
        if (!(type instanceof CodingType)) {
            throw new Error('The "type" must be instance of CodingType');
        }
        this.type = type;
        this.name = name;
    }

    toString() {
        let s = '';
        if (this.caption) {
            s += `@Caption("${this.caption}")\n`;
        }
        for (let annotation of this.annotations) {
            if (!annotation.startsWith('@')) {
                annotation = '@' + annotation;
            }
            s += `${annotation}\n`;
        }
        if (this.constant) {
            s += this.name + ',\n';
        } else {
            s += `private ${this.type.toString()} ${this.name};\n`;
        }
        return s;
    }

    getter() {
        if (this.constant) {
            return null;
        }
        let method = new CodingMethod('get' + this.name.firstToUpperCase());
        method.returnType = this.type;
        method.body = `return this.${this.name};`;
        return method;
    }

    setter() {
        if (this.constant) {
            return null;
        }
        let method = new CodingMethod('set' + this.name.firstToUpperCase());
        method.addParameter(this.type, this.name);
        method.body = `this.${this.name} = ${this.name};`;
        return method;
    }

}
